-- Drop Existing Procedures

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_Insert]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_Insert]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_Update]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_Update]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_InsertUpdate]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_InsertUpdate]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_Delete]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_Delete]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_DeleteDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_DeleteDynamic]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_Load]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_Load]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_SelectDynamic]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_SelectDynamic]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_SelectAll]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_SelectAll]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_SelectBy_EmployeeID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_SelectBy_EmployeeID]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_SelectBy_ProjectID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_SelectBy_ProjectID]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_SelectBy_AttachmentID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_SelectBy_AttachmentID]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_DeleteBy_EmployeeID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_DeleteBy_EmployeeID]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_DeleteBy_ProjectID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_DeleteBy_ProjectID]

IF OBJECT_ID(N'[dbo].[p_ACTP_Document_DeleteBy_AttachmentID]') IS NOT NULL
	DROP PROCEDURE [dbo].[p_ACTP_Document_DeleteBy_AttachmentID]


GO


------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_Insert]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_Insert]
	@ModuleID uniqueidentifier,
	@ProjectID int,
	@EmployeeID int,
	@AttachmentID int,
	@ID int OUTPUT
AS

INSERT INTO [dbo].[t_ACTP_Documents]
(
	[ModuleID],
	[ProjectID],
	[EmployeeID],
	[AttachmentID]
)
VALUES 
(
	@ModuleID,
	@ProjectID,
	@EmployeeID,
	@AttachmentID
)

SET @ID = SCOPE_IDENTITY()

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_Update]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_Update]
	@ID int,
	@ModuleID uniqueidentifier,
	@ProjectID int,
	@EmployeeID int,
	@AttachmentID int
AS

UPDATE
	[dbo].[t_ACTP_Documents]
SET
	[ModuleID] = @ModuleID,
	[ProjectID] = @ProjectID,
	[EmployeeID] = @EmployeeID,
	[AttachmentID] = @AttachmentID
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_InsertUpdate]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_InsertUpdate]
	@ID int,
	@ModuleID uniqueidentifier,
	@ProjectID int,
	@EmployeeID int,
	@AttachmentID int
AS
IF EXISTS(SELECT [ID] FROM [dbo].[t_ACTP_Documents] WHERE [ID] = @ID)
	BEGIN
		UPDATE
			[dbo].[t_ACTP_Documents] 
		SET
			[ModuleID] = @ModuleID,
			[ProjectID] = @ProjectID,
			[EmployeeID] = @EmployeeID,
			[AttachmentID] = @AttachmentID
		WHERE
			[ID] = @ID
	END
ELSE
	BEGIN
		
		INSERT INTO [dbo].[t_ACTP_Documents]
		(
			[ModuleID],
			[ProjectID],
			[EmployeeID],
			[AttachmentID]
		)
		VALUES 
		(
			@ModuleID,
			@ProjectID,
			@EmployeeID,
			@AttachmentID
		)		
	END
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_Delete]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_Delete]
	@ID int
AS

DELETE FROM 
	[dbo].[t_ACTP_Documents]
WHERE
	[ID] = @ID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_DeleteBy_EmployeeID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_DeleteBy_EmployeeID]
	@EmployeeID int
AS

DELETE FROM [dbo].[t_ACTP_Documents]
WHERE
	[EmployeeID] = @EmployeeID

GO

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_DeleteBy_ProjectID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_DeleteBy_ProjectID]
	@ProjectID int
AS

DELETE FROM [dbo].[t_ACTP_Documents]
WHERE
	[ProjectID] = @ProjectID

GO

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_DeleteBy_AttachmentID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_DeleteBy_AttachmentID]
	@AttachmentID int
AS

DELETE FROM [dbo].[t_ACTP_Documents]
WHERE
	[AttachmentID] = @AttachmentID

GO

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_DeleteDynamic]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_DeleteDynamic]
	@WhereCondition NVARCHAR(500)
AS

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 'DELETE FROM [dbo].[t_ACTP_Documents] WHERE ' + @WhereCondition

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_Load]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_Load]
	@ID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[EmployeeID],
	[AttachmentID]
FROM
	[dbo].[t_ACTP_Documents]
WHERE
	[ID] = @ID
GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_SelectBy_EmployeeID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_SelectBy_EmployeeID]
	@EmployeeID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[EmployeeID],
	[AttachmentID]
FROM
	[dbo].[t_ACTP_Documents]
WHERE
	[EmployeeID] = @EmployeeID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_SelectBy_ProjectID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_SelectBy_ProjectID]
	@ProjectID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[EmployeeID],
	[AttachmentID]
FROM
	[dbo].[t_ACTP_Documents]
WHERE
	[ProjectID] = @ProjectID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_SelectBy_AttachmentID]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_SelectBy_AttachmentID]
	@AttachmentID int
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[EmployeeID],
	[AttachmentID]
FROM
	[dbo].[t_ACTP_Documents]
WHERE
	[AttachmentID] = @AttachmentID

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_SelectDynamic]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_SelectDynamic]
	@WhereCondition NVARCHAR(500),
	@OrderByExpression NVARCHAR(250) = NULL
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

DECLARE @SQL NVARCHAR(MAX)

SET @SQL = 
'SELECT 
	[ID],
	[ModuleID],
	[ProjectID],
	[EmployeeID],
	[AttachmentID]
FROM [dbo].[t_ACTP_Documents] 
WHERE ' + @WhereCondition

IF @OrderByExpression IS NOT NULL AND LEN(@OrderByExpression) > 0
BEGIN
	SET @SQL = @SQL + ' ORDER BY ' + @OrderByExpression
END

EXEC sp_executesql @SQL

GO

------------------------------------------------------------------------------------------------------------------------
-- Stored procedure name: [dbo].[p_ACTP_Document_SelectAll]
-- Database: ASPNETVN.PORTAL
-- Author: Ngo Thanh Tung
-- Time created: Monday, October 04, 2010
------------------------------------------------------------------------------------------------------------------------

CREATE PROCEDURE [dbo].[p_ACTP_Document_SelectAll]
	@ModuleID UNIQUEIDENTIFIER
AS

SET NOCOUNT ON
SET TRANSACTION ISOLATION LEVEL READ COMMITTED

SELECT
	[ID],
	[ModuleID],
	[ProjectID],
	[EmployeeID],
	[AttachmentID]
FROM
	[dbo].[t_ACTP_Documents]	
WHERE 
	(ModuleID = @ModuleID)

GO

